import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { CreateUserDto } from './dto/create-user.dto';
import { RegisterUserDto } from './dto/register-user-dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { UserDocument } from './schema/user.schema';

@Injectable()
export class UserService {
  constructor(@InjectModel('User') private readonly userModel:Model<UserDocument>){}
  async register(registerUserDto:RegisterUserDto):Promise<UserDocument>{
    const result=await this.userModel.create((registerUserDto))
    return result
    
  }
  // 根据用户名和密码创建用户
  async create(registerUserDto: RegisterUserDto):Promise<UserDocument>{
    return await this.userModel.create((registerUserDto))
  }
  // 查找所有用户
  async findAll():Promise<UserDocument[]>{
    return await this.userModel.find().exec();
  }
  // 通过用户名查找用户
  async findOneByUsername(username: string):Promise<UserDocument|null>{
    return await this.userModel.findOne({username:username})
  }
  // 通过用户名和密码查找用户
  async findOneByUsernameAndPassword(username: string,password:string):Promise<UserDocument|null>{
    return await this.userModel.findOne({username:username,password:password})
  }
  // 更新用户信息
  async update(updateUserDto: UpdateUserDto) {
    return (await this.userModel.updateOne({_id:updateUserDto.id},{$set:updateUserDto}).exec()).acknowledged
  }

  remove(id: number) {
    return `This action removes a #${id} user`;
  }
}
